const http = require('../../../utils/httpUtils.js')
const app = getApp();
Page({
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '管理地址' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        idDefault: true
    },
    onLoad(options) {
        // console.log(options)
        this.setData({
            Type: options.type != undefined ? options.type : '',
            mold: options.mold
        })
    },
    onShow() {
        http._headerGet('/api/userConsign/address_list', {
            page: 1, //页数
            mold: this.data.mold, //user|用户，shop|商家
        }, res => {
            if (res.data.code == 1) {
                this.setData({
                    addressList: res.data.data
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }

        })
    },
    //判断选择地址
    selectTap(e) {
        if (this.data.Type == 'selete') {
            let data = this.data.addressList[e.currentTarget.id]
            //更新上个选择界面内容
            let pages = getCurrentPages()
            let prevPage = pages[pages.length - 2]
            let upData = {}
            upData.uc_id = data.uc_id //收货地址ID
            upData.uc_consignee = data.uc_consignee //收件人姓名
            upData.uc_phone = data.uc_phone //收件人手机号
            upData.uc_province = data.uc_province
            upData.uc_city = data.uc_city
            upData.uc_county = data.uc_county
            upData.uc_location = data.uc_location
            // console.log(upData)
            prevPage.setData({
                address: upData
            })
            wx.navigateBack({})
        }
    },
    //新增或修改地址2
    addAddess(e) {
        if (e.currentTarget.id != '') {
            wx.navigateTo({
                url: "/pages/public/add-address/index?data=" + JSON.stringify(this.data.addressList[e.currentTarget.id])
            })
        } else {
            wx.navigateTo({
                url: "/pages/public/add-address/index"
            })
        }
        return
    },
    //设置默认地址
    setDefault(e) {
        wx.showModal({
            title: '提示',
            content: '您确定要设置为默认地址吗？',
            success: res => {
                if (res.confirm) {
                    http._headerPost('/api/userConsign/address_default', {
                        uc_id: e.currentTarget.id,
                        mold: 'user'
                    }, res => {
                        if (res.data.code == 1) {
                            this.onShow()
                            wx.showToast({
                                title: '设置成功',
                            })
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none'
                            })
                        }
                    })
                }
            }
        })
    },
    //删除地址
    Delete(e) {
        http._headerPost('/api/userConsign/address_del', {
            uc_id: e.currentTarget.id
        }, res => {
            if (res.data.code == 1) {
                wx.showToast({
                    title: '删除成功',
                    icon: 'none'
                })
                this.onShow()
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
        })
    }
})